<?php
/**
 * Create by PhpStorm
 * User: lijia
 * Data: 2020/8/10
 * Time: 10:52
 */

namespace app\api\business;


use app\api\controller\Common;
use app\api\model\Goods;
use think\facade\Db;
use think\facade\Request;

class GoodsBus extends Common
{
    public $model;
    public $url;
    public function __construct()
    {
        $this->url = Request::scheme() . '://' . Request::host();
        $this->model = new Goods();
    }

    /**
     * 所有商品
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAll()
    {
        $goods =  $this->model->where('status',1)->field('id,cover,title,origin_price,present_price,deposit_price')->select();
        foreach ($goods as $key => $val) {
            $goods[$key]['cover'] = $this->url . $val['cover'];
        }
        return $goods;
    }


    /**
     * 商品列表
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList()
    {
        $category = Db::name('category')->order('sort', 'asc')->field('category_id,category_name')->select()->toArray();
        foreach ($category as $k => $v) {
            $where = ['status' => 1, 'cid' => $v['category_id']];
            $goods = $this->model->where($where)->field('id,cover,title,origin_price,present_price')->select();
            foreach ($goods as $key => $val) {
                $goods[$key]['cover'] = $this->url . $val['cover'];
            }
            $category[$k]['goods'] = $goods;
        }
        return $category;
    }


    /**
     *
     * @param $id
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function info($id)
    {
        $info = $this->model->find($id);
        if ($info == null) {
            return [];
        }
        $info['cover'] = $this->url . $info['cover'];
        if (isset($info['images'])){
            $info['images'] = json_decode($info['images']);
            $images = [];
            foreach ($info['images'] as $k => $v) {
                $images[] = $this->url.$v;
            }
            unset($info['images']);
            $info['images'] = $images;
        }

        $info['content'] = htmlspecialchars_decode($info['content']);
        $url = Request::scheme() . '://' . Request::host();
        $info['content'] = preg_replace('/(<img.+?src=")(.*?)/', '$1' . $url . '$2', $info['content']);
        return $info;
    }

    /**
     * 推荐
     * @param $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function recommend($id){
        $cid = Db::name('goods')->where('id', $id)->value('cid');
        $goods1 = Db::name('goods')->where('cid', $cid)->where('status',1)->where('id','<>', $id)->select()->toArray();
        $num = 4 - count($goods1);
        if ($num > 0){
            $goods2 = Db::name('goods')->where('status', 1)->where('id','<>', $id)->limit($num)->select()->toArray();
        }else{
            $goods2 = [];
        }
        $goods = array_merge($goods1, $goods2);
        foreach ($goods as $k => $v) {
            $goods[$k]['cover'] = $this->url . $v['cover'];
        }
        return $goods;
    }

}